package tank.cor;

import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;

import tank.GameObject;
import tank.PropertyMgr;

//碰撞器链条(责任链模式)
// implements Collider,可以chain.add(chain)
public class ColliderChain implements Collider{
	private List<Collider> colliders = new LinkedList<Collider>();
	public ColliderChain() {
//		add(new BullteTankCollider());
//		add(new TankTankCollider());
		String colliders = (String)PropertyMgr.get("colliders");
		String[] colliderStrs = colliders.split(",");
		for (String collider : colliderStrs) {
			try {
				add((Collider)Class.forName(collider).getDeclaredConstructor().newInstance());
			} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
					| InvocationTargetException | NoSuchMethodException | SecurityException
					| ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
	}
	public void add(Collider c) {
		colliders.add(c);
	}
	public boolean collide(GameObject o1, GameObject o2) {
		for(int i=0;i<colliders.size();i++) {
			boolean result = colliders.get(i).collide(o1, o2);
			if(result) {
				return true;
			}
		}
		return false;
	}
}
